﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProductKeyManager.DB;
using System.Xml.Linq;
using System.Xml;
using System.IO;
using System.Globalization;

namespace ProductKeyManager.Xml
{
    /// <summary>
    /// Xml Handler for Products Key Manager Xml
    /// </summary>
    public class ProductKeyManagerXmlHandler : IXmlHandler
    {
        #region IXmlHandler Members

        private List<Product> _Products;

        /// <summary>
        /// Gets the products imported from the XML.
        /// </summary>
        /// <value>The products.</value>
        public IEnumerable<Product> Products
        {
            get { return _Products; }
        }

        /// <summary>
        /// Imports the XML.
        /// </summary>
        /// <param name="fileUri">The file URI.</param>
        public void ImportXml(Uri fileUri)
        {
            _Products = new List<Product>();
            XmlDocument doc = new XmlDocument();
            doc.Load(fileUri.ToString());
            XmlNode node = doc.SelectSingleNode("ProductKeys");
            XmlNodeList Products = node.ChildNodes;
            foreach (XmlNode prod in Products)
            {
                string Name = prod.Attributes["Name"].Value;
                string Notes = string.Empty;
                if (prod.Attributes["Notes"] != null)
                {
                    Notes = string.IsNullOrEmpty(prod.Attributes["Notes"].Value) ? string.Empty : prod.Attributes["Notes"].Value;
                }
                string Icon = string.Empty;
                if (prod.Attributes["Icon"] != null)
                {
                    Icon = string.IsNullOrEmpty(prod.Attributes["Icon"].Value) ? string.Empty : prod.Attributes["Icon"].Value;
                }
                Product newProd = new Product
                {
                    Name = Name,
                    Notes = Notes,
                    Icon = Icon
                };

                foreach (XmlNode key in prod.ChildNodes)
                {
                    newProd.Keys.Add(new ProductKeyManager.DB.Key { Code = key.Attributes["KeyCode"].Value, AllocatedTo = key.Attributes["AllocatedTo"].Value });
                }
                _Products.Add(newProd);
            }

            if (_Products.Count() <= 0)
            {
                _Products = null;
            }
        }

        private string _GeneratedXml;

        /// <summary>
        /// Gets the XML generated by from the products.
        /// </summary>
        /// <value>The generated XML.</value>
        public string GeneratedXml
        {
            get { return _GeneratedXml; }
        }

        /// <summary>
        /// Generates the XML.
        /// </summary>
        /// <param name="products">The products.</param>
        public void GenerateXml(IEnumerable<Product> products)
        {
            GenerateXml(products, false, "");
        }

        /// <summary>
        /// Generates the XML.
        /// </summary>
        /// <param name="products">The products.</param>
        /// <param name="generateStyleSheet">if set to <c>true</c> the stylesheet is generated.</param>
        /// <param name="fileName">Name of the file.</param>
        public void GenerateXml(IEnumerable<Product> products, bool generateStyleSheet, string fileName)
        {
            string xmlResult = "<?xml version=\"1.0\" standalone=\"yes\" ?>" + Environment.NewLine;
            if (generateStyleSheet && !String.IsNullOrEmpty(fileName))
            {
                xmlResult += "<?xml-stylesheet type=\"text/xsl\" href=\"" + fileName + ".xsl\"?>" + Environment.NewLine;
            }
            XElement xml = new XElement("ProductKeys",
                               from p in products
                               select new XElement("Product",
                                          new XAttribute("Name", p.Name),
                                          new XAttribute("Notes", string.IsNullOrEmpty(p.Notes) ? string.Empty : p.Notes),
                                          new XAttribute("Icon", string.IsNullOrEmpty(p.Icon) ? string.Empty : p.Icon),
                                          from k in p.Keys
                                          select new XElement("Key",
                                                     new XAttribute("KeyCode", k.Code),
                                                     new XAttribute("AllocatedTo", k.AllocatedTo)
                                                     )
                                                 )
                                      );

            _GeneratedXml = xmlResult + xml.ToString();
        }

        #endregion
    }
}
